<?php
/**
* @version $Id: Heriniaina Eugene  04/05/2006 17.23 $
* @package mod_newsboxes
* @copyright Copyright (C) 2006 serasera.org
* @comment This modules shows news boxes like in CNN.com
* URL : http://hery.blaogy.org/2007/03/22/joomla-newsbox-module/
*/

// no direct access
defined( '_VALID_MOS' ) or die( 'Restricted access' );

$option = trim( mosGetParam( $_REQUEST, 'option', null ) );
$task = trim( mosGetParam( $_REQUEST, 'task', null ) );
$id = intval( mosGetParam( $_REQUEST, 'id', null ) );

global $mosConfig_cachepath, $_MAMBOTS;

$now 		= _CURRENT_SERVER_TIME;
$nullDate 	= $database->getNullDate();
$noauth 			= !$mainframe->getCfg( 'shownoauth' );
$nbx_sectionid =  intval($params->def('nbx_sectionid', 0));
$nbx_rows =  intval($params->def('nbx_rows', 2));
$nbx_columns =  intval($params->def('nbx_columns', 1));
$nbx_showtitles =  intval($params->def('nbx_showtitles', 1));
$nbx_showintro =  intval($params->def('nbx_showintro', 1));
$nbx_introlength =  intval($params->def('nbx_introlength', 300));
$nbx_introformat =  $params->def('nbx_introformat', 'Text');
$nbx_readmore =  intval($params->def('nbx_readmore', 1));
$nbx_titlelink =  intval($params->def('nbx_titlelink', 1));
$nbx_showimage =  intval($params->def('nbx_showimage', 1));
$nbx_imageinside =  intval($params->def('nbx_imageinside', 1));
$nbx_ordering =  intval($params->def('nbx_ordering', 1));
$nbx_showcattitle =  intval($params->def('nbx_showcattitle', 0));
$nbx_showcatlink =  intval($params->def('nbx_showcatlink', 0));

$nbx_catid =  $params->def('nbx_catid', 0);

//for multiple categories
$nbx_cat_array = explode(",", $nbx_catid);

if($nbx_sectionid == 0 && $nbx_cat_array[0] == 0) {
	echo "This module is not yet configured... Please configure it or remove it.";
	return;
}

if(count($nbx_cat_array) >= 1) {

	for($ii=0; $ii<count($nbx_cat_array); $ii++) {
		//adding catid & sectionid thanx to greg
		//http://hery.blaogy.org/2007/03/22/joomla-newsbox-module/#comment-288
		$query = "SELECT a.id, a.introtext, a.images, a.fulltext, a.attribs, a.title, a.state, cc.title as cat_title, a.sectionid, a.catid "
		."\n FROM #__content AS a"
		."\n INNER JOIN #__categories AS cc ON cc.id = a.catid"
		."\n INNER JOIN #__sections AS s ON s.id = a.sectionid"
		."\n WHERE a.state = 1 "
		."\n AND ( a.publish_up = " . $database->Quote( $nullDate ) . " OR a.publish_up <= " . $database->Quote( $now ) . " )"
		."\n AND ( a.publish_down = " . $database->Quote( $nullDate ) . " OR a.publish_down >= " . $database->Quote( $now ) . " )"
		. ( $noauth ? "\n AND a.access <= " . (int) $my->gid . " AND cc.access <= " . (int) $my->gid . " AND s.access <= " . (int) $my->gid : '' )
		. ($nbx_sectionid > 0  ? "\n AND a.sectionid = " . $nbx_sectionid : "")
		. (intval($nbx_cat_array[$ii]) > 0 ? " AND a.catid = " . $nbx_cat_array[$ii] : "" )
		. "\n AND cc.published = 1"
		."\n AND s.published = 1 ORDER BY "
		. ($nbx_ordering == 1 ? "\n a.ordering " : "\n a.created DESC " )
		;
		$database->setQuery( $query, 0, $nbx_rows );
		$nbxcats[] = $database->loadObjectList();
	}
}

$col = 0;
$col2 = 0;

echo ($nbx_columns > 1 && count($nbxcats) > 1) ? "<table><tr><td valign=\"top\">" : "";
for($ii=0; $ii<count($nbxcats); $ii++) {
	$rows = $nbxcats[$ii];
		$col++;



		$nbx_last_cat = "";
		echo "<div class=\"" . $params->get( 'moduleclass_sfx') . "box\">";
		echo ($nbx_columns > 1 && count($nbxcats) <= 1) ? "<table><tr><td valign=\"top\">" : "";
		foreach($rows as $row) {

			//get itemid
			//simpler

			$Itemid = $mainframe->getItemid( $row->id, 0, 0);


			//more complex
			/*
			$bs 	= $mainframe->getBlogSectionCount();
			$bc 	= $mainframe->getBlogCategoryCount();
			$gbs 	= $mainframe->getGlobalBlogSectionCount();

			$Itemid = $mainframe->getItemid( $row->id, 0, 0, $bs, $bc, $gbs );
			*/

			$row->Itemid_link = "&amp;Itemid=" . $Itemid ;

			$col2++;
			if($nbx_showcattitle > 0 && $row->cat_title != $nbx_last_cat) {
				echo "<div class=\"category\">" . ($nbx_showcatlink > 0 ? "<a href='index.php?option=com_content&amp;task=category&amp;sectionid=" . $row->sectionid . "&amp;id=" . $row->catid . $row->Itemid_link . "'>" : "") . $row->cat_title .  ($nbx_showcatlink > 0 ? "</a>" : "") . "</div>";
				$nbx_last_cat = $row->cat_title;
			}

			$imagetag = "";
			if($nbx_showimage > 0 && $row->images != "") {

				$img_str = $row->images;
				// split on \n the images fields into an array
				$img_str = explode( "\n", $row->images );

				$img = trim( $img_str[0] );

					// split on pipe the attributes of the image
				if ( $img ) {
					$attrib = explode( '|', trim( $img ) );
					// $attrib[0] image name and path from /images/stories

					// $attrib[1] alignment
					if ( !isset($attrib[1]) || !$attrib[1] ) {
						$attrib[1] = '';
					}

					// $attrib[2] alt & title
					if ( !isset($attrib[2]) || !$attrib[2] ) {
						$attrib[2] = 'Image';
					} else {
						$attrib[2] = htmlspecialchars( $attrib[2] );
					}


					// assemble the <image> tag
					$imagetag = '<img src="'. $mosConfig_live_site .'/images/stories/'. $attrib[0] .'"'. $size;
					$imagetag .= ' hspace="6" alt="'. $attrib[2] .'" title="'. $attrib[2] .'" border="'. $border .'" />';

				}
			}


			$row->text = $row->introtext;

			$_MAMBOTS->loadBotGroup( 'content' );
			$results = $_MAMBOTS->trigger( 'onPrepareContent', array( &$row, &$params, $page ), true );

			echo "<div class=\"box\">";
			if($nbx_imageinside == 0) {
				echo $imagetag;
			}

			if($nbx_showtitles != 0) {
				echo  "<div class=\"title\">" ;
				if($nbx_titlelink != 0) {
					echo "<a href=\"" . sefRelToAbs( 'index.php?option=com_content&amp;task=view&amp;id=' . $row->id . $row->Itemid_link ) . "\">";
				}
				echo $row->title ;
				if($nbx_titlelink != 0) {
					echo "</a>";
				}

				echo  "</div>" ;

			}
			if($nbx_showintro != 0) {
				echo "<div class=\"text\">";

				if($nbx_imageinside == 1) {
					echo $imagetag;
				}

				if($nbx_introformat == 'Text') {
					if($nbx_introlength > 0) {
						echo (strlen(strip_tags($row->text)) > $nbx_introlength) ? substr(strip_tags($row->text), 0, $nbx_introlength) . "... " : strip_tags($row->text);

					} else {
						echo strip_tags($row->text);
					}
				} else {
					echo ampReplace( $row->text );
				}
				echo "</div>";
			}
			echo ($nbx_readmore != 0 ) ? "<div class=\"readmore\"><a href=\"" . sefRelToAbs( 'index.php?option=com_content&amp;task=view&amp;id=' . $row->id . $row->Itemid_link ) . "\" class=\"readon" . $params->get( 'moduleclass_sfx') . "\">" . _READ_MORE . "</a></div>": "";

			echo "</div>";

			echo (($nbx_columns > 1  && count($nbxcats) <= 1 ) && (floor($nbx_rows / $nbx_columns)  == $col2)) ? "</td><td valign=\"top\">" : "";

		}
		echo ($nbx_columns > 1 && count($nbxcats) <= 1) ? "</td></tr></table>" : "";

		echo "</div>";
		echo (($nbx_columns > 1  && count($nbxcats) > 1 ) && (floor(count($nbxcats) / $nbx_columns)  == $col)) ? "</td><td valign=\"top\">" : "";

}

echo ($nbx_columns > 1  && count($nbxcats) > 1) ? "</td></tr></table>" : "";




?>